﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;


namespace SkinableFormsLib
{
    public class LWLabel : Label, ILWControl
    {
        SolidBrush _fontBrush = (SolidBrush)Brushes.Black;
        SolidBrush _backBrush = (SolidBrush)Brushes.Black;
        Graphics _graphics;
        Bitmap _lwBitmap;
        Rectangle _lwRectangle;
        #region ILWControl Members

        Bitmap SkinableFormsLib.ILWControl.LWBitmap
        {
            get
            {
                return _lwBitmap;
            }
        }

        #endregion

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            if (DesignMode)
            {
                base.OnPaintBackground(pevent);
            }
        }
        internal bool _hasShadow=false;
        private Point _lwShadowShift;
        public Point LWShadowShift
        {
            get
            {
                return _lwShadowShift;
            }
            set
            {
                _lwShadowShift = value;
                if (!_lwShadowShift.IsEmpty && _lwShadowColor != Color.Transparent)
                {
                    _hasShadow = true;
                }
                else
                {
                    _hasShadow = false;
                }


            }
        }
        internal SolidBrush _shadowBrush = (SolidBrush)Brushes.Black;
        private Color _lwShadowColor = Color.Transparent;
        public Color LWShadowColor
        {
            get
            {
                return _lwShadowColor;
            }
            set
            {
                _lwShadowColor = value;
                
                if (!_lwShadowShift.IsEmpty && _lwShadowColor != Color.Transparent)
                {
                    _hasShadow = true;
                    _shadowBrush = new SolidBrush(_lwShadowColor);
                }
                else
                {
                    _hasShadow = false;
                }
            }
        }


        protected override void OnPaint(PaintEventArgs e)
        {

            if (DesignMode)
            {
                base.OnPaint(e);
                return;
            }

            if (_lwBitmap == null)
            {
                _lwBitmap = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
            }
            if (_graphics == null)
            {
                _graphics = Graphics.FromImage(_lwBitmap);
                _graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                _graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                _graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                _graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
                _graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

            }
            _graphics.Clear(Color.Transparent);
            if (!_lwTransparent)
            {
                _graphics.FillRectangle(_backBrush, _lwRectangle);
            }
            
            if (_hasShadow)
            {
                //TODO Add BLUR functionality
                _graphics.DrawString(Text, Font, _shadowBrush, _lwShadowShift.X, _lwShadowShift.Y, StringFormat.GenericDefault);
                
            }
            _graphics.DrawString(Text, Font, _fontBrush, 0, 0, StringFormat.GenericDefault);

        }



        protected override void Dispose(bool disposing)
        {
            if (_graphics != null)
                _graphics.Dispose();
            if (_lwBitmap != null)
                _lwBitmap.Dispose();
            if (_fontBrush != null)
                _fontBrush.Dispose();
            base.Dispose(disposing);

        }
        public override Color ForeColor
        {
            get
            {
                return base.ForeColor;
            }
            set
            {
                base.ForeColor = value;
                _fontBrush = new SolidBrush(ForeColor);
            }
        }

        public override Color BackColor
        {
            get
            {
                return base.BackColor;
            }
            set
            {
                base.BackColor = value;
                _backBrush = new SolidBrush(BackColor);

            }
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            _lwRectangle.Width = Width;
            _lwRectangle.Height = Height;
        }





        #region ILWControl Members

        private bool _lwTransparent = false;
        public bool LWTransparent
        {
            get
            {
                return _lwTransparent;
            }
            set
            {
                _lwTransparent = value;
            }
        }

        #endregion
    }
}
